package com.emma.service.impl;

import com.emma.dao.AddressDao;
import com.emma.entity.Address;
import com.emma.entity.PageBean;
import com.emma.entity.User;
import com.emma.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;
    @Autowired
    private HttpSession session;

    protected User getUser(){
        return (User)session.getAttribute("user");
    }

    @Override
    public PageBean<Address> find(Map<String, Object> condition) {
        int uid = getUser().getUid();

        int current = (int) condition.get("current");
        int size = (int) condition.get("size");
        String name = (String) condition.get("name");
        String address = (String) condition.get("address");

        int begin=(current-1)*size;
        condition.put("begin",begin);
        condition.put("uid",uid);
        condition.put("name",name);
        condition.put("address",address);


        List<Address> data = addressDao.findByUid(condition);
        System.out.println("业务层输出"+data);
        int count = addressDao.findCount(condition);
        System.out.println("count:"+count);

        return new PageBean<Address>(data,count,current,size);
    }

    @Override
    public int add(Address address) {
        Integer uid = getUser().getUid();
        address.setUid(uid);
        address.setIsdefault("0");
        return addressDao.add(address);
    }

    @Override
    public int update(Address address) {

        return addressDao.update(address);
    }

    @Override
    public int delete(int aid) {
        return addressDao.delete(aid);
    }

    @Override
    @Transactional
    public int changeDefault(int aid) {

        int uid = getUser().getUid();
        addressDao.isNotDefault(uid);
        addressDao.isDefault(aid);

        return 0;
    }
}
